
// define the item component
Vue.component('department', {
    template: '#department-template',
    props: {
        model: Object
    },
    data: function () {
        return {
            open: false
        }
    },
    computed: {
        isFolder: function () {
            return this.model.subDepartmentList &&
                this.model.subDepartmentList.length
        }
    },
    methods: {
        toggle: function (e) {
            if (this.isFolder) {
                this.open = !this.open
            }
            $(e.currentTarget).css('color','#3c8dbc');
            $('#ems4j_department').find('span').css('color','rgb(51, 51, 51)');
            console.log(this.model.name);
            console.log(departmentTitle);
            console.log("未定义："+table_employee_list);
            /*右侧当前部门名称*/
            Vue.set(departmentTitle,'name',this.model.name);
            Vue.set(departmentTitle,'id',this.model.id);
            var m = this;
            Vue.http.get('/ems4j/employee/getEmployeeListByDepId',{
                params:{
                    'depId':m.model.id
                }
            }).then(function (empRes) {
                console.log(empRes.body);
                console.log(table_employee_list);
                Vue.set(table_employee_list,'employee_list',empRes.body);
            },function () {
                console.log('部门（id:'+m.model.id+'）员工获取出错');
            });
        },
        changeType: function () {
            if (!this.isFolder) {
                Vue.set(this.model, 'subDepartmentList', []);
                console.log(this.model.id);
                this.addChild();
                this.open = true;
            }else{
                this.addChild();
            }
        },
        addChild: function () {
            // $('#add_new_department').click();
            $('#addDepartment').modal("show");
            var m = this;
            $('#new_department_btn').unbind('click');
            $('#new_department_btn').click(function(){
                $('#loading').show();
                console.log("父部门id："+m.model.id);
                console.log("部门名称"+$('#new_department_name').val());
                var ordernum = $('#new_department_ordernum').val();
                if (ordernum == '')return false;
                Vue.http.post('/ems4j/dep/addDepartment',{},{
                    params:{
                        'parentid':m.model.id,
                        'name':$('#new_department_name').val(),
                        'deptype':$('#new_department_deptype').val(),
                        'ordernum':ordernum
                    }
                }).then(function (response) {
                    if (response.body == "nologin"){window.location.href="../index.html";}
                    $('#msgContent').html("添加成功！");
                    console.log(response.body);
                    m.model.subDepartmentList.push({
                        id:response.body.id,
                        name: response.body.name
                    })
                    m.open = true;
                    $('#loading').hide();
                    $('#addDepartment').modal("hide");
                    $('#new_department_name').val('');
                    $('#new_department_ordernum').val('');

                    $('#msgModal').modal("show");
                    setTimeout(function () {
                        $('#msgModal').modal("hide");
                        $('#msgContent').html("");
                    },700);
                },function () {
                    $('#msgContent').html("添加失败！");
                    $('#loading').hide();
                    $('#addDepartment').modal("hide");
                    $('#new_department_name').val('');
                    $('#new_department_ordernum').val('');

                    $('#msgModal').modal("show");
                    setTimeout(function () {
                        $('#msgModal').modal("hide");
                        $('#msgContent').html("");
                    },1000);
                });

            })
        },
        mouseover:function (e) {
            // console.log($(e.currentTarget).css('color'));
            // this.backColor = $(e.currentTarget).css('color');
            // $(e.currentTarget).css('color','#00c0ef');
        },
        mouseout:function (e) {
            // if ($(e.currentTarget).css('color') == '#3c8dbc')
            // console.log(this.backColor);
            // e.currentTarget.style.color = this.backColor;
            // $(e.currentTarget).css('color',this.backColor);
        }
    }
});
